package com.hisan.freeride.common.utils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by admin on 2017/5/9.
 */

public class BuildParamters {
    private static String accesskey = "K4EE5CFRA3";
    private static String accessSecret = "f68cdf2d3271cf451cd6b7d2d1fd71a5";

    /**
     * 签名字符串
     */
    private static  String signStr;

    /**
     * 拼接字符串
     */
    private static String str;

//    /**
//     * 参数
//     */
//    private Map<String, Object> map = new HashMap<String, Object>();

//    public static Map<String, Object> getParams(Map<String, Object> map){
//        String signStr = buildSign(map);
//        map.put("accesskey", accesskey);
//        map.put("sign", signStr);
//        return map;
//    }

    /**
     * 生成签名字符串
     * @param map
     * @return
     */
    public static String buildSign(Map<String, String> map) {
         Map<String, String>maps =new HashMap<>();
        String tempStr = "";
        map.put(new String("accesskey"), accesskey);
        maps = sortMapByKey(map);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            tempStr = tempStr + entry.getValue().toString();
        }
        str = tempStr + accessSecret;
        Tools tools = new Tools();
        signStr = tools.stringToMD5(str);
        return signStr;
    }


    /**
     * 根据Map的Key排序
     * @param
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
        sortMap.putAll(map);
        return sortMap;
    }
}

/**
 * 比较器
 */
class MapKeyComparator implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {
        return str1.compareTo(str2);
    }
}
